package com.rpframework.website.device.act.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.io.FilenameUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.rpframework.core.api.FileService;
import com.rpframework.core.exception.AdminIllegalArgumentException;
import com.rpframework.core.utils.TagUtils;
import com.rpframework.utils.DateUtils;
import com.rpframework.utils.NumberUtils;
import com.rpframework.utils.Pager;
import com.rpframework.website.device.domain.Bank;
import com.rpframework.website.device.domain.User;
import com.rpframework.website.device.service.BankService;

@Controller
@RequestMapping("/admin/bank")
public class AdminBankAct extends AdminAct{
	@Resource BankService service;
	@Resource FileService fileService;
	
	/**
	 * 列表
	 * */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	@RequestMapping("/list")
	public String list(@RequestParam(value="pager", required=false) Pager pager, Map<Object, Object> model, RedirectAttributes attr){
		if(pager == null) {
			pager = new Pager();
		}
		pager = service.getPager(pager);
		model.put("pager", pager);
		return this.doPackageURI("bank/list");
	}
	/**
	 * 添加银行卡
	 * */
	@RequestMapping("/{id}/edit")
	public String edit(@PathVariable Integer id, Map<Object, Object> model,RedirectAttributes attr){
		Bank t = service.select(id);
		if(t == null) {
			throw new AdminIllegalArgumentException("不存在的ID:" + id);
		}
		model.put("BankPager", t);
		return this.add(attr, model);
	}

	@RequestMapping("/add")
	public String add(RedirectAttributes attr, Map<Object, Object> model){
		return this.doPackageURI("bank/edit");
	}
	/**
	 * 删除
	 * */
	@RequestMapping("/{id}/delete")
	public String delete(@PathVariable Integer id, Map<Object, Object> model,RedirectAttributes attr){
		Bank t = service.select(id);
		if(t == null) {
			throw new AdminIllegalArgumentException("不存在的ID:" + id);
		}
		service.delete(id);
		setInfoMsg("删除操作成功！", attr);
		return redirect("/admin/bank/list");
	}
	
	@RequestMapping("/dosave")
	public String doSaveOrUpdate(@ModelAttribute Bank t, 
			@RequestParam(value="coverPath", required=false) MultipartFile coverPath,
			RedirectAttributes attr){
		if(coverPath!=null && coverPath.getSize() > 0) {
			try {
				String relativelyMaplinePath = "/device/bank/" + DateUtils.nowDate(DateUtils.YYYYMMDDHHMMSS) + NumberUtils.random() + "." + FilenameUtils.getExtension(coverPath.getOriginalFilename());
				fileService.saveFile(coverPath.getInputStream(), relativelyMaplinePath); 
				t.setImage(relativelyMaplinePath);
			} catch (Exception e) {
				throw new IllegalArgumentException("文件上传失败，原因:" + e.getLocalizedMessage());
			}
		} else {
			
		}
		boolean flag = service.doSaveOrUpdate(t);
		if(flag){
			setInfoMsg("新增操作成功！", attr);
		}else{
			setInfoMsg("添加失败！", attr);
		}
		return redirect("list");
	}
}
